package com.jokins.admin.vue.user.action;

import com.jokins.admin.vue.user.actioncontext.GetLoginStatsDoc;
import com.jokins.admin.vue.user.mapper.SysUserMapper;
import com.jokins.admin.vue.user.vo.LoginStatsVo;
import com.jokins.common.actions.action.ActionContext;
import com.jokins.common.actions.action.BaseAction;
import com.jokins.common.actions.exception.BusinessException;
import com.jokins.common.entry.vue.user.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class GetLoginStatsDocAction extends BaseAction<GetLoginStatsDoc> {

    @Autowired(required = true)
    private SysUserMapper sysUserMapper;

    @Override
    protected void process(GetLoginStatsDoc actionContext) throws BusinessException {
        List<SysUser> userList = sysUserMapper.selectUserInfo(null);
        List<LoginStatsVo> loginStatsVoList = userList.stream()
                .map(user -> {
                    LoginStatsVo vo = new LoginStatsVo();
                    BeanUtils.copyProperties(user, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        log.info("Fetched users: {}", loginStatsVoList);
        actionContext.setResult(ActionContext.RESULT_DOC_KEY, loginStatsVoList);
    }
}
